package servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import users.Subscription;

import dao.ReaderDAO;
import dao.SubscriptionDAO;
import dao.hibernate.ReaderDAOHibernateImpl;
import dao.hibernate.SubscriptionDAOHibernateImpl;

import model.Reader;

/**
 * Servlet implementation class ManageSubsServlet.
 */
public class ManageSubsServlet extends HttpServlet {
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** DAO to modify readers in the database. */
    private ReaderDAO readerDAO;
    /** DAO to modify subscriptions in the database. */
    private SubscriptionDAO subscriptionDAO;

    /**
     * Initializes  the servlet.
     *
     * @param config  configuration data of the servlet
     * @throws ServletException  if there is any problem
     */
    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);

        readerDAO = new ReaderDAOHibernateImpl();
        subscriptionDAO = new SubscriptionDAOHibernateImpl();
    }

    /**
     * Read the action the user wants to perform with what subscription.
     * If it is 'delete', delete the subscription.
     * Other options are not considered yet.
     *
     * Return the subscription view, updated.
     *
     * @param request  HTTP request
     * @param response  HTTP response
     * @throws ServletException  if there is any problem
     * @throws IOException  if there is any problem
     * @see HttpServlet#doGet(HttpServletRequest request,
     *                           HttpServletResponse response)
     */
    @Override
    protected void doGet(final HttpServletRequest request,
            final HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String topicName = request.getParameter("subscription");
        Reader reader = (Reader) request.getSession().getAttribute("reader");
        if (action.equals("Delete")) {
            Subscription sub = reader.getSubscription(topicName);
            try {
                reader.removeSubscription(topicName);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            subscriptionDAO.removeSubscription(sub);
            readerDAO.updateReader(reader);
            getServletContext().getRequestDispatcher(
                    "/jsp/manageSubscriptions.jsp").forward(request, response);
        } else if (action.equals("Modify")) {
            //TODO
        } else {
            throw new ServletException("Action not recognized");
        }
    }

    /**
     * @param request  HTTP request
     * @param response  HTTP response
     * @throws ServletException  if there is any problem
     * @throws IOException  if there is any problem
     * @see HttpServlet#doPost(HttpServletRequest request,
     *                            HttpServletResponse response)
     */
    @Override
    protected void doPost(final HttpServletRequest request,
            final HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
